package com.zhkc.wisdom.common.mybatis;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.zhkc.wisdom.common.configure.constant.ConfigureConstant;
import com.zhkc.wisdom.common.mybatis.encrypt.interceptor.ParameterInterceptor;
import com.zhkc.wisdom.common.mybatis.encrypt.interceptor.ResultSetInterceptor;
import com.zhkc.wisdom.common.mybatis.generator.CustomerIdGenerator;
import com.zhkc.wisdom.common.mybatis.plugins.MybatisLikeSqlInterceptor;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.annotation.Order;

/**
 * Project: mc Package: com.ttsh.mc.config.mybatisplus Description: ...
 * <p>
 *
 * @author Mars Data: 2018/5/3 下午4:16
 */

@Slf4j
@Configuration
@ConditionalOnProperty(prefix = ConfigureConstant.ZHKC_PROPERTIES_PREFIX + ".mybatis", name = "enabled", havingValue = "true", matchIfMissing = false)
@ConditionalOnClass({MybatisLikeSqlInterceptor.class})
public class MybatisAutoConfiguration {

    @PostConstruct
    public void init() {
        log.info("init MybatisAutoConfiguration...");
    }

    @Bean
    @ConditionalOnMissingBean(MybatisPlusInterceptor.class)
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.POSTGRE_SQL));
        return interceptor;
    }


    /**
     * mybatis/mybatis-plus模糊查询语句特殊字符转义配置
     *
     * @return MybatisLikeSqlInterceptor
     */
    @Bean
    public MybatisLikeSqlInterceptor mybatisSqlInterceptor() {
        return new MybatisLikeSqlInterceptor();
    }

    /**
     * mybatis/mybatis-plus字段加解密
     *
     * @return ParameterInterceptor
     */
    @Bean
    public ParameterInterceptor parameterInterceptor() {
        return new ParameterInterceptor();
    }

    /**
     * mybatis/mybatis-plus字段加解密
     *
     * @return ResultSetInterceptor
     */
    @Bean
    public ResultSetInterceptor resultSetInterceptor() {
        return new ResultSetInterceptor();
    }

    @Bean
    public IdentifierGenerator idGenerator() {
        return new CustomerIdGenerator();
    }

}
